package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SetLine;
import com.ruoyi.system.domain.SetStation;
import com.ruoyi.system.domain.SetStationAtlas;
import com.ruoyi.system.mapper.SetStationAtlasMapper;
import com.ruoyi.system.mapper.SetStationMapper;
import com.ruoyi.system.service.ISetStationAtlasService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 工位-电枪Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class SetStationAtlasServiceImpl implements ISetStationAtlasService
{
    private static final Logger log = LoggerFactory.getLogger(SetStationAtlasServiceImpl.class);

    @Autowired
    private SetStationAtlasMapper setStationAtlasMapper;

    @Autowired
    private SetStationMapper setStationMapper;

    /**
     * 查询工位-电枪
     * 
     * @param id 工位-电枪主键
     * @return 工位-电枪
     */
    @Override
    public SetStationAtlas selectSetStationAtlasById(Long id)
    {
        return setStationAtlasMapper.selectSetStationAtlasById(id);
    }

    /**
     * 查询工位-电枪
     *
     * @param code 工位-电枪编码
     * @return 工位-电枪
     */
    public SetStationAtlas selectSetStationAtlasByCode(String code)
    {
        return setStationAtlasMapper.selectSetStationAtlasByCode(code);
    }

    /**
     * 查询工位-电枪列表
     * 
     * @param setStationAtlas 工位-电枪
     * @return 工位-电枪
     */
    @Override
    public List<SetStationAtlas> selectSetStationAtlasList(SetStationAtlas setStationAtlas)
    {
        return setStationAtlasMapper.selectSetStationAtlasList(setStationAtlas);
    }

    /**
     * 新增工位-电枪
     * 
     * @param setStationAtlas 工位-电枪
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSetStationAtlas(SetStationAtlas setStationAtlas)
    {
        setStationAtlas.setCreateTime(DateUtils.getNowDate());
        setStationAtlas.setCreateBy(SecurityUtils.getUsername());
        setStationAtlas.setStatus(UserConstants.NORMAL);
        return setStationAtlasMapper.insertSetStationAtlas(setStationAtlas);
    }

    /**
     * 修改工位-电枪
     * 
     * @param setStationAtlas 工位-电枪
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSetStationAtlas(SetStationAtlas setStationAtlas)
    {
        setStationAtlas.setUpdateTime(DateUtils.getNowDate());
        setStationAtlas.setUpdateBy(SecurityUtils.getUsername());
        return setStationAtlasMapper.updateSetStationAtlas(setStationAtlas);
    }

    /**
     * 批量删除工位-电枪
     *
     * @param ids 需要删除的工位-电枪主键
     * @return 结果
     */
    @Override
    public int deleteSetStationAtlasByIds(Long[] ids) {
        return setStationAtlasMapper.deleteSetStationAtlasByIds(ids);
    }

    /**
     * 删除工位-电枪信息
     * 
     * @param id 工位-电枪主键
     * @return 结果
     */
    @Override
    public int deleteSetStationAtlasById(Long id)
    {
        return setStationAtlasMapper.deleteSetStationAtlasById(id);
    }

    /**
     * 导入工位-电枪数据
     *
     * @param stationAtlasList 工位-电枪数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importStationAtlas(List<SetStationAtlas> stationAtlasList, boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(stationAtlasList) || stationAtlasList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SetStationAtlas setStationAtlas : stationAtlasList)
        {
            try
            {
                // 验证是否存在这个工位-电枪
                SetStation ss = setStationMapper.selectSetStationByCode(setStationAtlas.getStationCode());
                List<SetStationAtlas> ssas = setStationAtlasMapper.selectSetStationAtlasList(setStationAtlas);

                if(StringUtils.isNull(ss)){
                    failureNum++;
                    failureMsg.append("<br/> " + failureNum + "、工位-电枪的工位编码" + setStationAtlas.getStationCode() + " 不存在");
                }else{
                    if (ssas.size() == 0)
                    {
                        setStationAtlas.setCreateBy(operName);
                        this.insertSetStationAtlas(setStationAtlas);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、工位-电枪 " + setStationAtlas.getStationCode() + " 导入成功");
                    }
                    else if (isUpdateSupport)
                    {
                        setStationAtlas.setUpdateBy(operName);
                        this.updateSetStationAtlas(setStationAtlas);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、工位-电枪 " + setStationAtlas.getStationCode() + " 更新成功");
                    }
                    else
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、工位-电枪 " + setStationAtlas.getStationCode() + " 已存在");
                    }
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、工位-电枪 " + setStationAtlas.getStationCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
